<?php

class Login extends Controller {
	public function html() {
		//已登录跳转到转入页面
		$mUser = new User_model();
		$currUser = $mUser->currUser();
		if($currUser) header('Location: '.($this->backurl()?$this->backurl():'/user/'));
		
		$data = array();$tpl = 'user/login.html';
		$this->display($data, $tpl);
	}
	public function json() {
		$email = $this->post('email', 'str');
		$password = $this->post('password', 'str');
		
		if(!$email) $this->displayJSON(array('status' => -1, 'message' => '请输入邮箱'));
		if(!$password) $this->displayJSON(array('status' => -2, 'message' => '请输入密码'));
		
		$mUser = new User_model();
		$result = $mUser->login($email, $password);
		if(is_array($result)) $this->displayJSON(array('status' => 0, 'message' => '登录成功', 'user' => $result));
		if(false === $result) $this->displayJSON(array('status' => -5, 'message' => '登录失败'));
		
		switch ($result) {
			case -1 :
				$json = array('status' => -3, 'message' => '该邮箱未注册');
				break;
			case -2 :
				$json = array('status' => -4, 'message' => '邮箱或者密码错误');
				break;
			case -3 :
				$json = array('status' => -6, 'message' => '账号未激活');
				break;
			default:
				$json = array('status' => 4000, 'message' => '未知错误');
		}
		$this->displayJSON($json);
	}
}